package com.yh.csx.bsf.ods.meta;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 *  列类
 *  @author Robin.Wang
 *  @date	2020-05-28
 * */

public class OdsColumn {
	public static final Pattern COLOUMN_TYPE_PATTERN=Pattern.compile("(^[A-Za-z]+)\\((\\d+)(,?)((\\d?))\\)$");
	/**
	 * 字段名
	 * */
	private String columnName;
	/**
	 * 原始位置（顺序）
	 * */
	private Integer ordinalPosition;
	/**
	 * 默认值
	 * */
	private String columnDefault;
	/**
	 * 是否为空
	 * */
	private boolean isNullable;
	/**
	 * 数据类型
	 * */
	private String dataType;	
	/**
	 * 最大长度
	 * */
	private Long characterMaxmunLength;
	/**
	 * 字节长度
	 * */
	private Long characterOctetLength;
	/**
	 * 数值精度
	 * */
	private Integer  numericPrecision;
	/**
	 * 数值刻度
	 * */
	private Integer numericScale;
	/**
	 * 日期精度
	 * */
	private Integer datetimePrecision;
	/**
	 * 字符编码集名称
	 * */
	private String characterSetName;
	/**
	 * 排序规则名称
	 * */
	private String collationName;
	/**
	 * 列类型（数据类型+长度/精度/刻度）
	 * */
	private String columnType;
	/**
	 * 键值
	 * 空,UNI,PRI,MUL
	 * */
	private String columnKey;
	/**
	 * 备注
	 * */
	private String comment;
	/**
	 * 其他
	 * **/
	private String extra;
	/**
	 * 值
	 * */
	private Object value;
	
	
	public boolean isNullable() {
		return isNullable;
	}
	public OdsColumn setNullable(boolean isNullable) {
		this.isNullable = isNullable;
		return this;
	}
	public String getColumnName() {
		return columnName;
	}
	public OdsColumn setColumnName(String columnName) {
		this.columnName = columnName;
		return this;
	}
	public Integer getOrdinalPosition() {
		return ordinalPosition;
	}
	public OdsColumn setOrdinalPosition(Integer ordinalPosition) {
		this.ordinalPosition = ordinalPosition;
		return this;
	}
	public String getColumnDefault() {
		return columnDefault;
	}
	public OdsColumn setColumnDefault(String columnDefault) {
		this.columnDefault = columnDefault;
		return this;
	}
	public String getDataType() {
		return dataType;
	}
	public OdsColumn setDataType(String dataType) {
		this.dataType = dataType;
		return this;
	}
	public Long getCharacterMaxmunLength() {
		return characterMaxmunLength;
	}
	public OdsColumn setCharacterMaxmunLength(Long characterMaxmunLength) {
		this.characterMaxmunLength = characterMaxmunLength;
		return this;
	}
	public Long getCharacterOctetLength() {
		return characterOctetLength;
	}
	public OdsColumn setCharacterOctetLength(Long characterOctetLength) {
		this.characterOctetLength = characterOctetLength;
		return this;
	}
	public Integer getNumericPrecision() {
		return numericPrecision;
	}
	public OdsColumn setNumericPrecision(Integer numericPrecision) {
		this.numericPrecision = numericPrecision;
		return this;
	}
	public Integer getNumericScale() {
		return numericScale;
	}
	public OdsColumn setNumericScale(Integer numericScale) {
		this.numericScale = numericScale;
		return this;
	}
	public Integer getDatetimePrecision() {
		return datetimePrecision;
	}
	public OdsColumn setDatetimePrecision(Integer datetimePrecision) {
		this.datetimePrecision = datetimePrecision;
		return this;
	}
	public String getCharacterSetName() {
		return characterSetName;
	}
	public OdsColumn setCharacterSetName(String characterSetName) {
		this.characterSetName = characterSetName;
		return this;
	}
	public String getCollationName() {
		return collationName;
	}
	public OdsColumn setCollationName(String collationName) {
		this.collationName = collationName;
		return this;
	}
	public String getColumnType() {
		return columnType;
	}
	public OdsColumn setColumnType(String columnType) {
		this.columnType = columnType;
		return this;
	}
	public String getColumnKey() {
		return columnKey;
	}
	public OdsColumn setColumnKey(String columnKey) {
		this.columnKey = columnKey;
		return this;
	}
	public String getComment() {
		return comment;
	}
	public OdsColumn setComment(String comment) {
		this.comment = comment;
		return this;
	}
	public String getExtra() {
		return extra;
	}
	public OdsColumn setExtra(String extra) {
		this.extra = extra;
		return this;
	}	

	public Object getValue() {
		return value;
	}
	public OdsColumn setValue(Object value) {
		this.value = value;
		return this;
	}


	public static class OdsColumnBuilder{
		public static OdsColumn build() {
			return new OdsColumn();
		}
	}

	public OdsColumn parseColumnType() {
		Matcher mather=COLOUMN_TYPE_PATTERN.matcher(this.getColumnType());
		if(mather.find()) {
			String type=mather.group(1).toLowerCase();
			if("decimal".equals(type)||"float".equals(type)||"double".equals(type)) {
				this.setNumericPrecision(Integer.valueOf(mather.group(2)));
				this.setNumericScale(Integer.valueOf(mather.group(4)));
			}
			else
			{
				this.setCharacterMaxmunLength(Long.valueOf(mather.group(2)));
			}
		}
		
		return this;
	}
	
	
	public OdsColumn() {
		super();
	}
	
	
	public OdsColumn(String columnName, boolean isNullable, String dataType) {
		super();
		this.columnName = columnName;
		this.isNullable = isNullable;
		this.dataType = dataType;
	}
	public OdsColumn(String columnName, boolean isNullable, String dataType,String defaultValue) {
		super();
		this.columnName = columnName;
		this.isNullable = isNullable;
		this.dataType = dataType;
		this.value=defaultValue;
	}
	
	public OdsColumn(String columnName, Integer ordinalPosition, String columnDefault, boolean isNullable,
			String dataType, Long characterMaxmunLength, Long characterOctetLength, Integer numericPrecision,
			Integer numericScale, Integer datetimePrecision, String characterSetName, String collationName,
			String columnType, String columnKey, String comment, String extra, String value) {
		super();
		this.columnName = columnName;
		this.ordinalPosition = ordinalPosition;
		this.columnDefault = columnDefault;
		this.isNullable = isNullable;
		this.dataType = dataType;
		this.characterMaxmunLength = characterMaxmunLength;
		this.characterOctetLength = characterOctetLength;
		this.numericPrecision = numericPrecision;
		this.numericScale = numericScale;
		this.datetimePrecision = datetimePrecision;
		this.characterSetName = characterSetName;
		this.collationName = collationName;
		this.columnType = columnType;
		this.columnKey = columnKey;
		this.comment = comment;
		this.extra = extra;
		this.value = value;
	}
	
	
}
